package com.zhangfei.order.server.grpc;

import com.zhangfei.order.biz.assember.OrderServerAssembler;
import com.zhangfei.order.biz.manager.OrderManager;
import com.zhangfei.order.proto.order1.OrderServer;
import com.zhangfei.order.proto.order1.OrderServiceGrpc;
import io.grpc.stub.StreamObserver;
import net.devh.boot.grpc.server.service.GrpcService;

import javax.annotation.Resource;

@GrpcService
public class OrderGrpcServer extends OrderServiceGrpc.OrderServiceImplBase {

    @Resource
    OrderManager orderManager;

    @Resource
    OrderServerAssembler orderServerAssembler;

    @Override
    public void confirm(OrderServer.OrderConfirmRequest request, StreamObserver<OrderServer.OrderConfirmResponse> responseObserver)  {
        //1.获取参数
        String productId=  orderServerAssembler.convertGrpcToDto(request);


        //2.manager处理逻辑
        boolean result = orderManager.confirm(productId);

        //3.响应组装
        String dtoTOGrpc = orderServerAssembler.convertTDtoTOGrpc(result);


        //4、构建返回值，这个可以写一个工具封装一下，这里不写了，方便大家了解原生语法
        OrderServer.OrderConfirmResponse response = OrderServer.OrderConfirmResponse.newBuilder()
                .setCodeInfo(OrderServer.CodeInfo.newBuilder().setCode(200).setMsg("执行成功").build())
                .setData(dtoTOGrpc)
                .build();
        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }







}
